package com.hsj.springcloudribbon;

import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import com.netflix.loadbalancer.ZoneAwareLoadBalancer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@SpringBootApplication
@RibbonClients
@RestController
public class SpringCloudRibbonApplication {

    @Autowired
    private SpringClientFactory springClientFactory;

    @GetMapping("/test")
    public void test(){
        ZoneAwareLoadBalancer<Server> lb = (ZoneAwareLoadBalancer<Server>) springClientFactory.getLoadBalancer("myclient");
//        List<Server> allServers = lb.getAllServers();
        ServerList<Server> serverList = lb.getServerListImpl();

        List<Server> initialListOfServers = serverList.getInitialListOfServers();
        if (CollectionUtils.isEmpty(initialListOfServers)){
            System.out.println("no server");
        }else{
            for (Server server : initialListOfServers) {
                System.out.println(server.getHostPort());
            }
        }

    }

    public static void main(String[] args) {
        SpringApplication.run(SpringCloudRibbonApplication.class, args);
    }

}
